package leetcode.tree;

import leetcode.tree.common.TreeNode;

/**
 * https://leetcode-cn.com/problems/range-sum-of-bst/
 *
 * @author mazouri
 * @create 2021-09-27 22:50
 */
public class _938_rangeSumBST {
    int low;
    int high;
    int sum = 0;

    public int rangeSumBST(TreeNode root, int low, int high) {
        this.low = low;
        this.high = high;
        dfs(root);
        return sum;
    }

    private void dfs(TreeNode root) {
        if (root == null) return;

        dfs(root.left);
        if (root.val >= low && root.val <= high) {
            sum += root.val;
        }
        dfs(root.right);
    }
}
